package coreBiz

import (
	"fmt"
	"net/url"
)

// HttpBuildQuery 实现类似 PHP 的 http_build_query 功能
func HttpBuildQuery(data map[string]any) string {
	values := url.Values{}

	for key, val := range data {
		switch v := val.(type) {
		case string:
			values.Add(key, v)
		case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
			values.Add(key, fmt.Sprintf("%v", v))
		case []string:
			for _, item := range v {
				values.Add(key, item)
			}
		case []any:
			for _, item := range v {
				values.Add(key, fmt.Sprintf("%v", item))
			}
		case map[string]any:
			for subKey, subVal := range v {
				values.Add(fmt.Sprintf("%s[%s]", key, subKey), fmt.Sprintf("%v", subVal))
			}
		default:
			values.Add(key, fmt.Sprintf("%v", v))
		}
	}

	return values.Encode()
}
